package com.jwaoo.chat.viewholder;

import android.graphics.Color;
import android.widget.ImageView;
import android.widget.TextView;
import com.dx.as.common.sys.TimeUtil;
import com.jwaoo.chat.R;
import com.jwaoo.chat.attachment.FileAttachment;
import com.jwaoo.chat.session.avchat.AVChatState;
import com.jwaoo.chat.session.avchat.AVChatType;
import com.jwaoo.chat.ui.recyclerview.adpter.BaseMultiItemFetchLoadAdapter;

/**
 * Created by zhoujianghua on 2015/8/6.
 */
public class MsgViewHolderAVChat extends MsgViewHolderBase {

    private ImageView typeImage;
    private TextView statusLabel;

    public MsgViewHolderAVChat(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
        return R.layout.nim_message_item_avchat;
    }

    @Override
    protected void inflateContentView() {
        typeImage = findViewById(R.id.message_item_avchat_type_img);
        statusLabel = findViewById(R.id.message_item_avchat_state);
    }

    @Override
    protected void onItemClick() {

    }

    @Override
    protected void bindContentView() {
//        if (message.getAttachment() == null) {
//            return;
//        }
        layoutByDirection();

        refreshContent();
    }

    private void layoutByDirection() {
        if (isReceivedMessage()) {
            if (message.getMsgtype() == AVChatType.AUDIO.getValue()) {
                typeImage.setImageResource(R.drawable.avchat_left_type_audio);
            } else {
                typeImage.setImageResource(R.drawable.avchat_left_type_video);
            }
            statusLabel.setTextColor(context.getResources().getColor(R.color.color_grey_999999));
        } else {
            if (message.getMsgtype() == AVChatType.AUDIO.getValue()) {
                typeImage.setImageResource(R.drawable.avchat_right_type_audio);
            } else {
                typeImage.setImageResource(R.drawable.avchat_right_type_video);
            }
            statusLabel.setTextColor(Color.WHITE);
        }
    }

    private void refreshContent() {
        FileAttachment fileAttachment = message.getAttachment();
        String textString = "";
        if (message.getAttachment().getAttachstate() == AVChatState.request.getValue()){
            textString = TimeUtil.secToTime(0);
        }else if (message.getAttachment().getAttachstate() == AVChatState.cancel.getValue()){
            textString = "已取消";
        }else if (message.getAttachment().getAttachstate() == AVChatState.busy.getValue()){
            textString = "对方正忙";
        }else if (message.getAttachment().getAttachstate() == AVChatState.hangup.getValue()){
            textString = TimeUtil.secToTime((int)fileAttachment.getDuration());
        }else if (message.getAttachment().getAttachstate() == AVChatState.refused.getValue()){
            textString = context.getString(R.string.avchat_refused);
        }else if (message.getAttachment().getAttachstate() == AVChatState.missed.getValue()){
            textString = context.getString(R.string.avchat_no_pick_up);
        }
        statusLabel.setText(textString);
    }
}
